MAXREFDES82# Code Documentation  V01.00
Maxim Smart Force Sensor
 All Files Functions Variables Macros Groups Pages
usbd_cdc_interface.c
Go to the documentation of this file.
1 
28 /* Includes ------------------------------------------------------------------*/
29 #include "main.h"
30 
31 /* Private typedef -----------------------------------------------------------*/
32 /* Private define ------------------------------------------------------------*/
33 #define APP_RX_DATA_SIZE 32
34 #define APP_TX_DATA_SIZE 32
35 
36 extern uint8_t request;
37 
38 /* Private macro -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 USBD_CDC_LineCodingTypeDef LineCoding = {
41  115200, /* baud rate */
42  0x00, /* stop bits-1 */
43  0x00, /* parity - none */
44  0x08 /* nb. of bits 8 */
45  };
46 
47 uint8_t UserRxBuffer[APP_RX_DATA_SIZE];/* Received Data over USB are stored in this buffer */
48 uint8_t UserTxBuffer[APP_TX_DATA_SIZE];/* Received Data over UART (CDC interface) are stored in this buffer */
49 
50 uint32_t UserTxBufPtrIn = 0;/* Increment this pointer or roll it back to
51  start address when data are received over USART */
52 uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
53  start address when data are sent over USB */
54 
55 /* UART handler declaration */
56 UART_HandleTypeDef UartHandle;
57 /* TIM handler declaration */
58 TIM_HandleTypeDef TimHandle;
59 /* USB handler declaration */
60 extern USBD_HandleTypeDef USBD_Device;
61 
62 /* Private function prototypes -----------------------------------------------*/
63 static int8_t CDC_Itf_Init(void);
64 static int8_t CDC_Itf_DeInit(void);
65 static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length);
66 static int8_t CDC_Itf_Receive(uint8_t* pbuf, uint32_t *Len);
67 static void Error_Handler(void);
68 static void ComPort_Config(void);
69 static void TIM_Config(void);
70 
71 USBD_CDC_ItfTypeDef USBD_CDC_fops =
72 {
73  CDC_Itf_Init,
74  CDC_Itf_DeInit,
75  CDC_Itf_Control,
76  CDC_Itf_Receive
77 };
78 
79 /* Private functions ---------------------------------------------------------*/
80 
86 static int8_t CDC_Itf_Init(void)
87 {
88  /*##-1- Configure the UART peripheral ######################################*/
89  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
90  /* USART configured as follow:
91  - Word Length = 8 Bits
92  - Stop Bit = One Stop bit
93  - Parity = No parity
94  - BaudRate = 115200 baud
95  - Hardware flow control disabled (RTS and CTS signals) */
96  UartHandle.Instance = USARTx;
97  UartHandle.Init.BaudRate = 115200;
98  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
99  UartHandle.Init.StopBits = UART_STOPBITS_1;
100  UartHandle.Init.Parity = UART_PARITY_NONE;
101  UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
102  UartHandle.Init.Mode = UART_MODE_TX_RX;
103  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
104  if(HAL_UART_Init(&UartHandle) != HAL_OK)
105  {
106  /* Initialization Error */
107  Error_Handler();
108  }
109 
110  /*##-2- Put UART peripheral in IT reception process ########################*/
111  /* Any data received will be stored in "UserTxBuffer" buffer */
112  if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)UserTxBuffer, 1) != HAL_OK)
113  {
114  /* Transfer error in reception process */
115  Error_Handler();
116  }
117 
118  /*##-3- Configure the TIM Base generation #################################*/
119  TIM_Config();
120 
121  /*##-4- Start the TIM Base generation in interrupt mode ####################*/
122  /* Start Channel1 */
123  if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
124  {
125  /* Starting Error */
126  Error_Handler();
127  }
128 
129  /*##-5- Set Application Buffers ############################################*/
130  USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0);
131  USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer);
132 
133  return (USBD_OK);
134 }
135 
142 static int8_t CDC_Itf_DeInit(void)
143 {
144  /* DeInitialize the UART peripheral */
145  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
146  {
147  /* Initialization Error */
148  Error_Handler();
149  }
150  return (USBD_OK);
151 }
152 
161 static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
162 {
163  switch (cmd)
164  {
165  case CDC_SEND_ENCAPSULATED_COMMAND:
166  /* Add your code here */
167  break;
168 
169  case CDC_GET_ENCAPSULATED_RESPONSE:
170  /* Add your code here */
171  break;
172 
173  case CDC_SET_COMM_FEATURE:
174  /* Add your code here */
175  break;
176 
177  case CDC_GET_COMM_FEATURE:
178  /* Add your code here */
179  break;
180 
181  case CDC_CLEAR_COMM_FEATURE:
182  /* Add your code here */
183  break;
184 
185  case CDC_SET_LINE_CODING:
186  LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\
187  (pbuf[2] << 16) | (pbuf[3] << 24));
188  LineCoding.format = pbuf[4];
189  LineCoding.paritytype = pbuf[5];
190  LineCoding.datatype = pbuf[6];
191 
192  /* Set the new configuration */
193  ComPort_Config();
194  break;
195 
196  case CDC_GET_LINE_CODING:
197  pbuf[0] = (uint8_t)(LineCoding.bitrate);
198  pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
199  pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
200  pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
201  pbuf[4] = LineCoding.format;
202  pbuf[5] = LineCoding.paritytype;
203  pbuf[6] = LineCoding.datatype;
204  break;
205 
206  case CDC_SET_CONTROL_LINE_STATE:
207  /* Add your code here */
208  break;
209 
210  case CDC_SEND_BREAK:
211  /* Add your code here */
212  break;
213 
214  default:
215  break;
216  }
217 
218  return (USBD_OK);
219 }
220 
226 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
227 {
228  uint32_t buffptr;
229  uint32_t buffsize;
230 
232  {
233  if(UserTxBufPtrOut > UserTxBufPtrIn) /* Rollback */
234  {
235  buffsize = APP_RX_DATA_SIZE - UserTxBufPtrOut;
236  }
237  else
238  {
239  buffsize = UserTxBufPtrIn - UserTxBufPtrOut;
240  }
241 
242  buffptr = UserTxBufPtrOut;
243 
244  USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t*)&UserTxBuffer[buffptr], buffsize);
245 
246  if(USBD_CDC_TransmitPacket(&USBD_Device) == USBD_OK)
247  {
248  UserTxBufPtrOut += buffsize;
250  {
251  UserTxBufPtrOut = 0;
252  }
253  }
254  }
255 }
256 
262 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
263 {
264  /* Increment Index for buffer writing */
265  UserTxBufPtrIn++;
266 
267  /* To avoid buffer overflow */
269  {
270  UserTxBufPtrIn = 0;
271  }
272 
273  /* Start another reception: provide the buffer pointer with offset and the buffer size */
274  HAL_UART_Receive_IT(huart, (uint8_t *)(UserTxBuffer + UserTxBufPtrIn), 1);
275 }
276 
285 static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len)
286 {
287  HAL_UART_Transmit_DMA(&UartHandle, Buf, *Len);
288  return (USBD_OK);
289 }
290 
296 //original
297 
298 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
299 {
300  // Initiate next USB packet transfer once UART completes transfer (transmitting data over Tx line)
301  USBD_CDC_ReceivePacket(&USBD_Device);
302 
303  //real load data starts at UserRxBuffer[3].
304  request = UserRxBuffer[3];
305 }
306 
307 //Mulong
308 /*
309 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
310 {
311  //USBD_CDC_SetRxBuffer(&USBD_Device, &usbRxBuf[0]);
312 
313  // Initiate next USB packet transfer once UART completes transfer (transmitting data over Tx line)
314  USBD_CDC_ReceivePacket(&USBD_Device);
315 
316  //real load data starts at UserRxBuffer[3].
317  request = UserRxBuffer[3];
318 
319  if(UserRxBuffer[3] == 0xFF)
320  {
321  UserTxBuffer[0] = 0xFE;
322  UserTxBuffer[1] = MAJORVERSION;
323  UserTxBuffer[2] = MINORVERSION;
324  UserTxBuffer[3] = 0x12;
325  UserTxBuffer[4] = 0x13;
326  UserTxBuffer[5] = 0x14;
327  UserTxBuffer[6] = 0x15;
328  UserTxBuffer[7] = 0x16;
329  UserTxBuffer[8] = 0x17;
330  UserTxBuffer[9] = 0x18;
331 
332  if(USBD_Device.dev_state == USBD_STATE_CONFIGURED)
333  USBD_CDC_TransmitPacket(&USBD_Device);
334  }
335 }
336 */
344 static void ComPort_Config(void)
345 {
346  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
347  {
348  /* Initialization Error */
349  Error_Handler();
350  }
351 
352  /* set the Stop bit */
353  switch (LineCoding.format)
354  {
355  case 0:
356  UartHandle.Init.StopBits = UART_STOPBITS_1;
357  break;
358  case 2:
359  UartHandle.Init.StopBits = UART_STOPBITS_2;
360  break;
361  default :
362  UartHandle.Init.StopBits = UART_STOPBITS_1;
363  break;
364  }
365 
366  /* set the parity bit*/
367  switch (LineCoding.paritytype)
368  {
369  case 0:
370  UartHandle.Init.Parity = UART_PARITY_NONE;
371  break;
372  case 1:
373  UartHandle.Init.Parity = UART_PARITY_ODD;
374  break;
375  case 2:
376  UartHandle.Init.Parity = UART_PARITY_EVEN;
377  break;
378  default :
379  UartHandle.Init.Parity = UART_PARITY_NONE;
380  break;
381  }
382 
383  /*set the data type : only 8bits and 9bits is supported */
384  switch (LineCoding.datatype)
385  {
386  case 0x07:
387  /* With this configuration a parity (Even or Odd) must be set */
388  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
389  break;
390  case 0x08:
391  if(UartHandle.Init.Parity == UART_PARITY_NONE)
392  {
393  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
394  }
395  else
396  {
397  UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
398  }
399 
400  break;
401  default :
402  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
403  break;
404  }
405 
406  UartHandle.Init.BaudRate = LineCoding.bitrate;
407  UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
408  UartHandle.Init.Mode = UART_MODE_TX_RX;
409  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
410 
411  if(HAL_UART_Init(&UartHandle) != HAL_OK)
412  {
413  /* Initialization Error */
414  Error_Handler();
415  }
416 
417  /* Start reception: provide the buffer pointer with offset and the buffer size */
418  HAL_UART_Receive_IT(&UartHandle, (uint8_t *)(UserTxBuffer + UserTxBufPtrIn), 1);
419 }
420 
426 static void TIM_Config(void)
427 {
428  /* Set TIMx instance */
429  TimHandle.Instance = TIMx;
430 
431  /* Initialize TIM3 peripheral as follows:
432  + Period = (CDC_POLLING_INTERVAL * 10000) - 1
433  + Prescaler = ((APB1 frequency / 1000000) - 1)
434  + ClockDivision = 0
435  + Counter direction = Up */
436  TimHandle.Init.Period = (CDC_POLLING_INTERVAL*1000) - 1;
437  TimHandle.Init.Prescaler = 84-1;
438  TimHandle.Init.ClockDivision = 0;
439  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
440  if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
441  {
442  /* Initialization Error */
443  Error_Handler();
444  }
445 }
446 
452 void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
453 {
454  /* Transfer error occured in reception and/or transmission process */
455  Error_Handler();
456 }
457 
463 static void Error_Handler(void)
464 {
465  /* Add your own code here */
466 }
467 
468 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/